/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ZoneMainFragment.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-6 上午9:33:33   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shizhefei.view.indicator.FragmentListPageAdapter;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.IndicatorViewPager.IndicatorFragmentPagerAdapter;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.swift.maintenance.R;
import com.swift.utils.DeviceUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: ZoneMainFragment
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-6 上午9:33:33
 * 
 */
public class ZoneMainFragment extends Fragment {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	
	@BindView(R.id.moretab_indicator)
	ScrollIndicatorView indicator;
	@BindView(R.id.moretab_viewPager)
	ViewPager mViewPage;
	IndicatorViewPager indicatorViewPager;

	private LayoutInflater inflate;
	private String[] names = { "推荐", "活动", "群组", "好友" };
	private int size = 4;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_zone, null, false);
		ButterKnife.bind(this, view);
		setTop();
		inflate = LayoutInflater.from(getActivity());
		indicator.setScrollBar(new ColorBar(getActivity(), Color.parseColor("#16B0F4"),5));

		// 设置滚动监听
		int selectColorId = R.color.tab_top_on;
		int unSelectColorId = R.color.tab_top;
		int selectSizeId = R.dimen.tab_text_select;
		int unSelectSizeId = R.dimen.tab_text_noselect;
		
		indicator.setOnTransitionListener(new OnTransitionTextListener().setValueFromRes(getActivity(), selectColorId, unSelectColorId, selectSizeId, unSelectSizeId));

		indicatorViewPager = new IndicatorViewPager(indicator, mViewPage);
		indicatorViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
		indicatorViewPager.setPageOffscreenLimit(0);
		return view;
	}

	
	/**
	 * @author: HYM
	 * @Title: setTop
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-6
	 */
	private void setTop() {
		tvBack.setVisibility(View.INVISIBLE);
		Drawable searchDraw = getResources()
				.getDrawable(R.drawable.icon_search);
		searchDraw.setBounds(0, 0, searchDraw.getMinimumWidth(),
				searchDraw.getMinimumHeight());
		tvSearch.setCompoundDrawables(searchDraw, null, null, null);
		tvSearch.setText("");
	}
	
    
    private class MyAdapter extends IndicatorFragmentPagerAdapter {

		public MyAdapter(FragmentManager fragmentManager) {
			super(fragmentManager);
		}

		@Override
		public int getCount() {
			return size;
		}

		@Override
		public View getViewForTab(int position, View convertView,
				ViewGroup container) {
			if (convertView == null) {
				convertView = inflate.inflate(R.layout.tab_top, container,false);
			}
			TextView textView = (TextView) convertView;
			textView.setText(names[position % names.length]);
			textView.setPadding(20, 0, 20, 0);
			return convertView;
		}

		@Override
		public Fragment getFragmentForPage(int position) {
			Log.d("getFragmentForPage    ", "getFragmentForPage"+position);
			Fragment curFragment = null;
			switch (position) {
			case 0:
				// 推荐
				Log.d("dfsfsf    ", "推荐");
				curFragment = new ZoneTuiJianFragment();
				break;
			case 1:
				// 活动
				Log.d("dfsfsf    ", "活动");
//				curFragment = new ZoneHuoDongFragment();
				curFragment = new ZoneHuoDongFragmentNew();
				break;
			case 2:
				// 群组
				Log.d("dfsfsf    ", "群组");
				curFragment = new ZoneQunZuFragment();
				break;
			case 3:
				// 好友
				Log.d("dfsfsf    ", "好友");
				curFragment = new ZoneHaoYouFragment();
				break;
			default:
				break;
			}

			return curFragment;
		}

		@Override
		public int getItemPosition(Object object) {
			return FragmentListPageAdapter.POSITION_NONE;
		}

	};
	
	@OnClick({ R.id.tvSearch })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvSearch:
			Intent intent = new Intent(getActivity(),ZoneSearchActivity.class);
			int[] location = DeviceUtil.getInstance().getLocationOnScreen(tvSearch);
			int centerX = location[0];//获取当前位置的横坐标
			int centerY = location[1];//获取当前位置的纵坐标
			intent.putExtra("x", centerX);
			intent.putExtra("y", centerY);
			startActivity(intent);
			break;
		
		default:
			break;
		}
	}
}
